/***********************************************
 This file is part of redia-lib-utils-ios
.
 
 Copyright (c) 2014 Redia A/S
 
 Redia-library-app-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-library-app-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-library-app-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

#import "LazyLoadConnection.h"
#import "LazyLoadManager.h"
#import "UrlCacheImageEntry.h"
#import "UrlCacheManager.h"

@interface LazyLoadConnection ()

@property (strong, nonatomic) UrlCacheImageEntry *cacheEntry;

@end

@implementation LazyLoadConnection

- (id)initWithUrl:(NSString *)url setScaleforRetina:(BOOL)setScaleforRetina onSuccess:(LLSuccessBlock)successBlock onError:(LLErrorBlock)errorBlock onCancel:(LLCancelBlock)cancelBlock
{
    self = [super init];
    if (self) {
        self.url = url;
        self.successBlock = successBlock;
        self.errorBlock = errorBlock;
        self.cancelBlock = cancelBlock;
        self.scaleForRetina = setScaleforRetina;
    }
    return self;
}

- (void)reportErrorWithCode:(LazyLoaderErrorCode)errorCode message:(NSString*)errorMessage
{
    if (self.errorBlock) {
        self.errorBlock(errorCode, errorMessage);
    }
    [[LazyLoadManager instance] removeConnection:self];
}

- (void)cancel
{
    [self.cacheEntry cancelRequest];
    if (self.cancelBlock) {
        self.cancelBlock();
    }
    self.cacheEntry = nil;
}

- (void)start
{
    NSAssert(self.url != nil && self.successBlock != nil && self.errorBlock != nil, @"Required parameter missing. Use the specialized initializer.");

    if (self.cacheEntry) {
        [self reportErrorWithCode:LazyLoadUsageError message:@"This loader is already attempting to download."];
        return;
    }
    
    NSURL *url = [NSURL URLWithString:self.url];
    if (url == nil) {
        [self reportErrorWithCode:LazyLoadParameterError message:@"Invalid URL given."];
        return;
    }

    self.cacheEntry = [[UrlCacheManager instance] loadImageUrl:url delegate:self];
}


#pragma mark - UrlCacheEntryDelegate

-(void)cacheEntry:(UrlCacheEntry *)cacheEntry didLoadObjectFromUrl:(NSURL *)url ofType:(UrlCacheEntryType)type
{
    if (type == UrlCacheEntryTypeImage) {
        UrlCacheImageEntry* imageEntry = (UrlCacheImageEntry*) cacheEntry;
        UIImage* theImage = imageEntry.resultImage;
        if (self.successBlock) {
            if (self.scaleForRetina) {
                if (RD_IS_RETINA_SCREEN) {
                    theImage = [UIImage imageWithCGImage:theImage.CGImage scale:2.0 orientation:UIImageOrientationUp];
                }
            }
            self.successBlock(theImage);
        }
        
    }
    [[LazyLoadManager instance] removeConnection:self];
}

-(void)cacheEntry:(UrlCacheEntry *)cacheEntry didFailOnUrl:(NSURL *)url error:(NSError *)error
{
    [self reportErrorWithCode:LazyLoadNetworkError message:[NSString stringWithFormat:@"Error fetching image from url: %@, error: %@",url,error]];
}

/* was:
#pragma mark - ImageLoadedNotificationDelegate

- (void)imageLoaded:(UIImage *)theImage
{
    if (self.successBlock) {
        if (self.scaleForRetina) {
            if (RD_IS_RETINA_SCREEN) {
                theImage = [UIImage imageWithCGImage:theImage.CGImage scale:2.0 orientation:UIImageOrientationUp];
            }
        }
        self.successBlock(theImage);
    }
    [[LazyLoadManager instance] removeConnection:self];
}

- (void)loadImageError
{
    [self reportErrorWithCode:LazyLoadNetworkError message:@"Error fetching image."];
}

 */

@end
